home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / text / psorder-.000 / psorder- / psorder-0.1.0 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-07  |  3.5 KB  |  113 lines

  1. /* $Id: util.c,v 1.1.1.1 1996/06/07 01:00:45 sverrehu Exp $ */
  2. /**************************************************************************
  3.  *
  4.  *  FILE            util.c
  5.  *  MODULE OF       psorder - move negative pages in a .ps-file
  6.  *
  7.  *  DESCRIPTION     Misc. functions that may come handy in other programs
  8.  *                  later. :-)
  9.  *
  10.  *  WRITTEN BY      Sverre H. Huseby <sverrehu@ifi.uio.no>
  11.  *
  12.  **************************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <unistd.h>
  16.  
  17. #include <shhmsg.h>
  18.  
  19. #include "util.h"
  20.  
  21. /**************************************************************************
  22.  *                                                                        *
  23.  *                    P U B L I C    F U N C T I O N S                    *
  24.  *                                                                        *
  25.  **************************************************************************/
  26.  
  27. /*-------------------------------------------------------------------------
  28.  *
  29.  *  NAME          fileOpenReadSeekable
  30.  *
  31.  *  FUNCTION      Open a file for reading, possibly stdin.
  32.  *
  33.  *  SYNOPSIS      FILE *fileOpenReadSeekable(const char *filename);
  34.  *
  35.  *  INPUT         filename
  36.  *                        name of file to open, or NULL to use stdin.
  37.  *
  38.  *  RETURNS       An open file, aborts in case of error.
  39.  *
  40.  *  DESCRIPTION   If filename indicates stdin (ie. it equals NULL), a
  41.  *                temporary file is created. Anything coming from
  42.  *                stdin is put in this file, untill EOF is
  43.  *                reached. When that happens, the temporary file is
  44.  *                rewinded, and  returned. The temporary file will
  45.  *                automatically be removed when closed, or when the
  46.  *                program exits normally.
  47.  *
  48.  */
  49. FILE *fileOpenReadSeekable(const char *filename)
  50. {
  51.     FILE *ret;
  52.     int c;
  53.  
  54.     if (filename) {
  55.     /* open the given file */
  56.     msgVerbose(2, "opening %s for reading\n", filename);
  57.     if ((ret = fopen(filename, "r")) == NULL)
  58.         msgFatalPerror(filename);
  59.     } else {
  60.         /* read from stdin to a temporary file */
  61.     msgVerbose(2, "reading from stdin to a temporary file\n");
  62.     if ((ret = tmpfile()) == NULL)
  63.         msgFatalPerror("temporary file");
  64.     while ((c = getchar()) != EOF)
  65.         if (putc(c, ret) == EOF)
  66.         msgFatalPerror("temporary file");
  67.     rewind(ret);
  68.     }
  69.     return ret;
  70. }
  71.  
  72.  
  73.  
  74. /*-------------------------------------------------------------------------
  75.  *
  76.  *  NAME          fileOpenWrite
  77.  *
  78.  *  FUNCTION      Open a file for writing, possibly stdout.
  79.  *
  80.  *  SYNOPSIS      FILE *fileOpenWrite(const char *filename, int force);
  81.  *
  82.  *  INPUT         filename
  83.  *                        name of file to open, or NULL to use stdout.
  84.  *                force   force deletion of existing file? if not,
  85.  *                        abort the program if the file exists.
  86.  *
  87.  *  RETURNS       An open file, aborts in case of error.
  88.  *
  89.  *  DESCRIPTION   If filename indicates stdout (ie. it equals NULL),
  90.  *                nothing special is done.
  91.  *
  92.  *  NOTE          The opened file is neither seekable, nor readable.
  93.  *
  94.  */
  95. FILE *fileOpenWrite(const char *filename, int force)
  96. {
  97.     FILE *ret;
  98.  
  99.     if (filename) {
  100.     /* open the given file */
  101.     if (!force && access(filename, 0) == 0)
  102.         msgFatal("%s: file exists\n", filename);
  103.     msgVerbose(2, "opening %s for writing\n", filename);
  104.     if ((ret = fopen(filename, "w")) == NULL)
  105.         msgFatalPerror(filename);
  106.     } else {
  107.         /* write to stdout */
  108.     msgVerbose(2, "writing to stdout\n");
  109.     ret = stdout;
  110.     }
  111.     return ret;
  112. }
  113.